import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { NzMessageService } from 'ng-zorro-antd'

import { INewPageColumn } from '@shared/interfaces/common'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { MenuBasicComponent } from './basic/basic.component'
import { IMenu } from '@shared/interfaces/role'
import { MenuService } from '../menu.service'
import { PageService } from '@shared/services/page.service'
import { I18NService } from '@core'

@Component({
  selector: 'app-menu-new',
  templateUrl: './new.component.html',
})
export class MenuNewComponent implements OnInit {
  pages: INewPageColumn[] = []

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private msgSrv: NzMessageService,
    private menuService: MenuService,
    private pageService: PageService,
    private i18n: I18NService,
  ) {}

  ngOnInit() {
    this.pages = [
      {
        component: new ComWindow(MenuBasicComponent, { title: this.trans('MENUS.TITLE') }),
      },
    ]
  }

  submit(formData: IMenu): void {
    this.menuService.createMenu(formData).subscribe(resp => {
      if (resp) {
        this.pageService.triggerGuardAlive(false)
        this.msgSrv.success(this.trans('GENERAL.NOTIFICATION.CREATE_SUCCESS'))
        this.router.navigate(['../'], {
          relativeTo: this.route,
          // tell the next page that a new menu was created
          state: { isMenuCreated: true },
        })
      }
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
